// 引入axios
import axios from 'axios';
import router from "@/router";
import store from "@/store";
import Cookies from "js-cookie";

let baseUrl = process.env.VUE_APP_BASE_API;
// 创建axios实例
const httpService = axios.create({
    // url前缀-'http:xxx.xxx'
    // baseURL: process.env.BASE_API, // 需自定义
    baseURL: process.env.VUE_APP_BASE_API,
    // 请求超时时间
    timeout: 1000000, // 需自定义
    withCredentials: true  // 确保携带凭证
});

// 添加请求拦截器
httpService.interceptors.request.use(function (config) {
    // 在发送请求之前做些什么
    config.headers.AUTHORIZATION = Cookies.get('token');
    return config;
}, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
});

// 添加响应拦截器
httpService.interceptors.response.use(function (response) {
    // 对响应数据做点什么
    if (response.request.responseType === 'blob' || response.request.responseType === 'arraybuffer') {
        return response.data
    }
    if (response.config.responseType === 'blob' || response.config.responseType === 'arraybuffer') {
        return response.data;
    }
    if (response.data.code === 401) {
        // ElMessage.error('登录状态已过期，请重新登录');
        store.commit('RESET_TABS')
        localStorage.clear()
        Cookies.remove('token')
        router.replace('/login');
    }
    return response;
}, function (error) {
    // 对响应错误做点什么
    return Promise.reject(error);
});

/*网络请求部分*/

/*
 *  get请求
 *  url: 请求地址
 *  config: Axios配置对象
 * */
export function get(url, config = {}) {
    return new Promise((resolve, reject) => {
        httpService({
            url: url,
            method: 'get',
            ...config, // 展开所有配置项，包括params、headers、responseType等
            headers: {
                // 移除默认的Content-Type，由Axios自动处理
                ...config.headers
            }
        }).then(response => {
            resolve(response);
        }).catch(error => {
            reject(error);
        });
    });
}

/*
 *  post请求
 *  url:请求地址
 *  params:参数
 * */
export function post(url, data = {}, config = {}) {
    return new Promise((resolve, reject) => {
        httpService({
            url: url,
            method: 'post',
            data: data,
            ...config // 合并其他配置
        }).then(response => {
            resolve(response);
        }).catch(error => {
            reject(error);
        });
    });
}

export function put(url, data = {}, config = {}) {
    return new Promise((resolve, reject) => {
        httpService({
            url: url,
            method: 'put',
            data: data,
            ...config // 合并其他配置
        }).then(response => {
            resolve(response);
        }).catch(error => {
            reject(error);
        });
    });
}

/*
 *  delete请求
 *  url:请求地址
 *  params:参数
 * */
export function del(url, params = {}) {
    return new Promise((resolve, reject) => {
        httpService({
            url: url,
            method: 'delete',
            data: params
        }).then(response => {
            console.log(response)
            resolve(response);
        }).catch(error => {
            console.log(error)
            reject(error)
        });
    });
}


/*
 *  文件上传
 *  url:请求地址
 *  params:参数
 * */
export function fileUpload(url, params = {}) {
    return new Promise((resolve, reject) => {
        httpService({
            url: url,
            method: 'post',
            data: params,
            headers: {'Content-Type': 'multipart/form-data'}
        }).then(response => {
            resolve(response);
        }).catch(error => {
            reject(error)
        });
    });
}

export function getServerUrl() {
    return baseUrl;
}

export default {
    get,
    post,
    del,
    put,
    fileUpload,
    getServerUrl,
}

